//
//  HWRankListController.swift
//  HWPromoteProjectAPP
//
//  Created by 王昌健 on 2022/1/29.
//  Copyright © 2022 王昌健. All rights reserved.
//

import UIKit

class HWRankListController: HWBaseViewController {
    var collectionView:UICollectionView!
    let collectionProtocol = HWRankListProtocol()
    var type = HWMineNavType.popularityRank
    let httpManager = HWHttpManager()
    var mutDataArr = NSMutableArray()
    override func viewDidLoad() {
        super.viewDidLoad()

        initUI()
        
        initFrame()
        
        loadData()
        
        addRefresh()
        
    }

}

extension HWRankListController {
    private func initUI() -> Void {
        
        navigationView.titleLabel.text = "排行榜"
        if type == .popularityRank {
            navigationView.titleLabel.text = "人气榜"
        } else if type == .consumeRank {
            navigationView.titleLabel.text = "消费榜单"
        }
        let flowL = UICollectionViewFlowLayout.init()
        flowL.minimumLineSpacing = 0
        flowL.minimumInteritemSpacing = 0
        
        view.backgroundColor = .white
        collectionProtocol.type = type
        collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: flowL)
        collectionView.delegate = collectionProtocol
        collectionView.dataSource = collectionProtocol
        collectionView.showsVerticalScrollIndicator = false
        collectionView.backgroundColor = .white
        collectionView.register(HWHomeTopThreeRankCell.self, forCellWithReuseIdentifier: "HWHomeTopThreeRankCell")
        collectionView.register(HWHomeOtherRankCell.self, forCellWithReuseIdentifier: "HWHomeOtherRankCell")
        collectionView.register(HWHomeHeader.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "HWHomeHeader")
        collectionView.contentInsetAdjustmentBehavior = .never
        view.addSubview(collectionView)
        
        view.bringSubviewToFront(navigationView)
    }
    
    private func initFrame() -> Void {
        collectionView.snp.makeConstraints { make in
            make.top.equalTo(navigationView.snp.bottom)
            make.left.right.bottom.equalToSuperview()
        }
    }
}
extension HWRankListController {
    func addRefresh() -> () {
        
        collectionView.mj_header = MJRefreshNormalHeader()
        collectionView.mj_header?.setRefreshingTarget(self, refreshingAction: #selector(self.loadNewData))
        collectionView.mj_footer = MJRefreshBackNormalFooter()
        collectionView.mj_footer?.setRefreshingTarget(self, refreshingAction: #selector(self.loadMoreData))
    }
    
    
    @objc func loadNewData() -> () {
        loadData()
    }
    func loadData() -> Void {
        if type == .popularityRank {
            httpManager.getPopularityListInfo { infoModel in
                self.collectionView.mj_header?.endRefreshing()
                self.collectionView.mj_footer?.endRefreshing()
                if infoModel.count > 0 {
                    self.mutDataArr.removeAllObjects()
                    self.mutDataArr.addObjects(from: infoModel)
                }
                self.collectionProtocol.rankListModels = self.mutDataArr as? [HWRankListModel]
                self.collectionView.reloadData()
            } failure: { error in
                self.collectionView.mj_header?.endRefreshing()
                self.collectionView.mj_footer?.endRefreshing()
                self.view.showHUD(title: error)
            }
        }else if type == .consumeRank {
            httpManager.getConsumeRankInfo { infoModel in
                self.collectionView.mj_header?.endRefreshing()
                self.collectionView.mj_footer?.endRefreshing()
                if infoModel.count > 0 {
                    self.mutDataArr.removeAllObjects()
                    self.mutDataArr.addObjects(from: infoModel)
                }
                self.collectionProtocol.consumeRankModels = self.mutDataArr as? [HWConsumeRankModel]
                self.collectionView.reloadData()
            } failure: { error in
                self.collectionView.mj_header?.endRefreshing()
                self.collectionView.mj_footer?.endRefreshing()
                self.view.showHUD(title: error)
            }
        }
        
    }
    @objc func loadMoreData() -> () {
        collectionView.mj_footer?.endRefreshing()
        if type == .popularityRank {
            httpManager.getPopularityListInfoFooter { models in
                self.collectionView.mj_footer?.endRefreshing()
                if models.count > 0 {
                    self.mutDataArr.addObjects(from: models)
                }
                self.collectionProtocol.rankListModels = self.mutDataArr as? [HWRankListModel]
                self.collectionView.reloadData()
            } failure: { error in
                self.collectionView.mj_footer?.endRefreshing()
            }
        }else if type == .consumeRank {
            httpManager.getConsumeRankInfoFooter { models in
                self.collectionView.mj_footer?.endRefreshing()
                if models.count > 0 {
                    self.mutDataArr.addObjects(from: models)
                }
                self.collectionProtocol.consumeRankModels = self.mutDataArr as? [HWConsumeRankModel]
                self.collectionView.reloadData()
            } failure: { error in
                self.collectionView.mj_footer?.endRefreshing()
            }
        }
        
        
    }
}
